/*
Copyright 2021 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package pkg

import (
	"bytes"
	"io/ioutil"
	"path"
	"path/filepath"
	"strings"
	"text/template"

	"github.com/pkg/errors"
	log "github.com/sirupsen/logrus"
	versionutil "k8s.io/apimachinery/pkg/util/version"
)

func processWorkflows(settings *Settings, cfg *jobGroup, oldestVer, minVer *versionutil.Version) error {
	log.Infof("processing workflows for jobGroup %q", cfg.Name)

	var err error
	var tBytes []byte
	var templateWorkflow, templateFileName *template.Template
	tPath := cfg.KinderWorkflowSpec.Template

	// in case the template path is not defined, assume this job group has a single task file
	if len(cfg.KinderWorkflowSpec.Template) == 0 {
		goto copyAdditionalFiles
	}

	// prepare the workflow template
	if !path.IsAbs(tPath) {
		tPath = filepath.Join(filepath.Dir(settings.PathConfig), tPath)
	}
	tBytes, err = ioutil.ReadFile(tPath)
	if err != nil {
		return err
	}
	templateWorkflow, err = template.New("workflow-template").Funcs(template.FuncMap{
		"dashVer":    dashVer,
		"ciLabelFor": ciLabelFor,
	}).Parse(string(tBytes))
	if err != nil {
		return err
	}

	// prepare output file name template
	templateFileName, err = template.New("file-name").Parse(cfg.KinderWorkflowSpec.TargetFile)
	if err != nil {
		return err
	}

	for i, job := range cfg.Jobs {
		log.Infof("processing Job index %d, %#v", i, job)

		if skipVersion(oldestVer, minVer, job.KubernetesVersion) {
			log.Infof("skipping Job index %d, %#v", i, job)
			continue
		}

		// prepare variables for template replacement
		vars := templateVars{
			KubernetesVersion: job.KubernetesVersion,
			KubeadmVersion:    job.KubeadmVersion,
			KubeletVersion:    job.KubeletVersion,
			InitVersion:       job.InitVersion,
			TargetFile:        cfg.TestInfraJobSpec.TargetFile,
		}

		// prepare skip versions
		if len(job.SkipVersions) != 0 {
			vars.SkipVersions, err = parseSkipVersions(settings.KubernetesVersion, job.SkipVersions)
			if err != nil {
				return errors.Wrapf(err, "malformed SkipVersions %v", job.SkipVersions)
			}
		}

		// execute templates
		buf := bytes.Buffer{}
		if err := templateWorkflow.Execute(&buf, vars); err != nil {
			return err
		}
		str := buf.String()
		str = strings.ReplaceAll(str, "\\{", "{") // unescape existing template vars
		str = strings.ReplaceAll(str, "\\}", "}")

		buf.Reset()
		if err := templateFileName.Execute(&buf, vars); err != nil {
			return err
		}

		// add header and write output file
		outPath := filepath.Join(settings.PathWorkflows, buf.String())
		log.Infof("writing %q", outPath)
		str = autogeneratedHeader + "\n" + str
		if err := ioutil.WriteFile(outPath, []byte(str), 0664); err != nil {
			return err
		}
	}

copyAdditionalFiles:
	for _, taskFile := range cfg.KinderWorkflowSpec.AdditionalFiles {
		outPath := filepath.Join(settings.PathWorkflows, path.Base(taskFile))
		inPath := taskFile
		if !path.IsAbs(inPath) {
			inPath = filepath.Join(filepath.Dir(settings.PathConfig), taskFile)
		}
		log.Infof("copying %q to %q", inPath, settings.PathWorkflows)
		taskBytes, err := ioutil.ReadFile(inPath)
		if err != nil {
			return err
		}
		// add header and write
		str := autogeneratedHeader + "\n" + string(taskBytes)
		if err := ioutil.WriteFile(outPath, []byte(str), 0644); err != nil {
			return err
		}
	}
	return nil
}
